package com.yj.rselasticsearch.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yj.common.core.controller.BaseController;
import com.yj.common.core.domain.AjaxResult;
import com.yj.common.core.domain.entity.FileInfo;
import com.yj.common.core.domain.entity.WarningInfo;
import com.yj.rselasticsearch.domain.dto.FileInfoDto;
import com.yj.rselasticsearch.service.ElasticsearchService;
import com.yj.rselasticsearch.service.FileInfoService;
import com.yj.rselasticsearch.service.FileTypeService;
import com.yj.rselasticsearch.service.WarningInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.yj.rselasticsearch.domain.dto.WarningInfoDto;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * es搜索引擎
 *
 * @author luoy
 */
@Api("搜索引擎")
@RestController
@RequestMapping("es")
public class ElasticsearchController extends BaseController {
    @Resource
    private ElasticsearchService elasticsearchService;

    @Resource
    private WarningInfoService warningInfoService;

    @Resource
    private FileInfoService fileInfoService;

    @Resource
    private FileTypeService fileTypeService;


    /**
     * 创建索引
     */
    @ApiOperation("创建索引")
    @Deprecated
    @PutMapping("createIndex")
    public AjaxResult createIndex() {
        // 创建索引，会根据Item类的@Document注解信息来创建
        boolean isCreate = elasticsearchService.createIndexAndMapping(WarningInfo.class);
        return AjaxResult.success(isCreate);
    }

    /**
     * 删除索引
     */
    @ApiOperation("删除索引")
    @Deprecated
    @DeleteMapping("deleteIndex")
    public AjaxResult deleteIndex() {
        return AjaxResult.success(elasticsearchService.deleteIndex(WarningInfo.class));
    }

    /**
     * 批量新增
     */
    @ApiOperation("批量插入数据库告警信息")
    @Deprecated
    @GetMapping("insertBatch")
    public AjaxResult insertBatch() {
        List<WarningInfo> warningInfoList = warningInfoService.list();
        boolean result = elasticsearchService.insert(warningInfoList);
        return AjaxResult.success(result);
    }

    /**
     * 删除所有
     */
    @ApiOperation("删除所有索引")
    @Deprecated
    @DeleteMapping("deleteBatch")
    public void deleteBatch() {
        elasticsearchService.deleteBatch(WarningInfo.class);
    }


    /**
     * 告警信息关键词联想
     *
     * @param warningInfoDto
     * @return
     */
    @ApiOperation("关键词联想")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "用户当前手机号", required = true, dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "keyword", value = "关键词", required = true, dataType = "String", dataTypeClass = String.class)

    })
    @PostMapping("getAssociationalWord")
    public AjaxResult getAssociationalWord(@RequestBody WarningInfoDto warningInfoDto, HttpServletRequest request) {
        List<String> words = elasticsearchService.getAssociationalWord(warningInfoDto, request);
        return AjaxResult.success(words);
    }


    /**
     * 告警信息高亮分词分页查询
     *
     * @param warningInfoDto
     * @return
     */
    @ApiOperation("高亮分词分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词", required = true, dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageIndex", value = "页码", required = true, dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", dataTypeClass = Integer.class)
    })
    @PostMapping("queryHighLightWord")
    public AjaxResult queryHighLightWord(@RequestBody WarningInfoDto warningInfoDto, HttpServletRequest request) {
        IPage<WarningInfo> warningInfoListPage = elasticsearchService.queryHighLightWord(warningInfoDto, request);
        return AjaxResult.success(warningInfoListPage);
    }

    /**
     * 告警信息首页信息
     *
     * @return
     */
    @PostMapping("queryWarningInfoIndex")
    public AjaxResult queryWarningInfoIndex(@RequestBody WarningInfoDto warningInfoDto) {
        return warningInfoService.queryAllData(warningInfoDto);
    }

    /**
     * 文件内容信息关键词联想
     *
     * @param warningInfoDto
     * @return
     */
    @ApiOperation("关键词联想")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contenttype", value = "文档类型", required = true, dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "keyword", value = "关键词", required = true, dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("getAssociationalWordDoc")
    public AjaxResult getAssociationalWordDoc(@RequestBody WarningInfoDto warningInfoDto, HttpServletRequest request) {
        List<String> words = elasticsearchService.getAssociationalWordOther(warningInfoDto, request);
        return AjaxResult.success(words);
    }


    /**
     * 文件信息高亮分词分页查询
     *
     * @param warningInfoDto
     * @return
     */
    @ApiOperation("高亮分词分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键词", required = true, dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageIndex", value = "页码", required = true, dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "contenttype", value = "文档类型", required = true, dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("queryHighLightWordDoc")
    public AjaxResult queryHighLightWordDoc(@RequestBody WarningInfoDto warningInfoDto, HttpServletRequest request) {
        IPage<FileInfo> warningInfoListPage = elasticsearchService.queryHighLightWordOther(warningInfoDto, request);
        return AjaxResult.success(warningInfoListPage);
    }


    /**
     * 根据id查询单挑文档明细
     *
     * @param id
     * @return
     */
    @GetMapping("selectOneFileInfo")
    public AjaxResult selectOneFileInfo(Integer id) {
        return fileInfoService.selectOne(id);
    }


    /**
     * 分页查询文件信息
     *
     * @param fileInfo
     * @return
     */
    @PostMapping("pageSearchFileInfo")
    public AjaxResult pageSearchFileInfo(@RequestBody FileInfoDto fileInfo,HttpServletRequest request) {
        return fileInfoService.pageSearchFileInfo(fileInfo,request);
    }

    /**
     * 根据当前登录用户获取分类类型
     *
     * @return
     */
    @GetMapping("getFileTypeInfo")
    public AjaxResult getFileTypeInfo(HttpServletRequest request) {
        return fileTypeService.getFileTypeListByUser(request);
    }
}
